package com.jiuyun.servlet;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiuyun.dao.BookDao;
import com.jiuyun.dao.SettingsDao;
import com.jiuyun.dao.impi.BookDaoImpl;
import com.jiuyun.dao.impi.SettingsDaoimpl;
import com.jiuyun.entity.*;
import com.jiuyun.util.GetCode;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cho/*")
public class ChooseServlet extends BaseServlet{
    SettingsDao dao=new SettingsDaoimpl();
    BookDao bookDao=new BookDaoImpl();
    Gson gson=new Gson();
    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        PrintWriter out =response.getWriter();
        List<Book> list=null;
        try {
            list=bookDao.allbook();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String j=gson.toJson(list);
        out.print(j);
        out.flush();
        out.close();
    }


    protected void findq(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname = request.getParameter("bookname");
        System.out.println(bookname);
        List<Question> list = null;
        int bookid = 0;
        try {
            list = dao.alltimu(bookname);
            bookid=bookDao.findbookid(bookname);
            request.getSession().setAttribute("findq", list);
            response.sendRedirect("/Eventstar/settings_zw/choose.jsp");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void addtm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid =request.getParameter("tm");//选中的题目id
        System.out.println(pid);
        String code=(String)request.getSession().getAttribute("CODE");//创建考试时的考试题库编码
        List<Integer> zhi=GetCode.Getid(pid);//调用方法获得编码对象
        List<Question> list = null;
        List<Subjec> list2= null;
        try {
            Code c=dao.getbm(code);
            list2=dao.setm(code);
            for(Integer i:zhi){
                int r=0;
                for(Subjec sub:list2){
                    if(sub.getQuestion_id()==i){
                        r=1;
                    }
                }
                if(r==0){
                    Subjec s=new Subjec(c.getCode_id(),i);
                    dao.addSubjec(s);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    protected void gettm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code=(String)request.getSession().getAttribute("CODE");//创建考试时的考试题库编码
        System.out.println(code);
        List<Question> list = null;
        try {
            list=dao.gettimu(code);
            System.out.println(list);
            request.getSession().setAttribute("GETTM",list);
            //request.getRequestDispatcher("/book_lwj/book_in.jsp").forward(request, response)
            response.sendRedirect("http://localhost:9090/Eventstar/settings_zw/settings.jsp?URL=settings_zw/edit.jsp");
           //request.getRequestDispatcher("/settings_zw/settings.jsp?URL=settings_zw/edit.jsp").forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void up(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int qid=Integer.parseInt(request.getParameter("q_id"));
        int fs=Integer.parseInt(request.getParameter("fs"));
        String bmid=(String) request.getSession().getAttribute("CODE");
        System.out.println(bmid);
        try {
            dao.upsuj(fs,bmid,qid);
            response.sendRedirect("http://localhost:9090/Eventstar/settings_zw/settings.jsp?URL=settings_zw/edit.jsp");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int qid=Integer.parseInt(request.getParameter("q_id"));
        String bmid=(String) request.getSession().getAttribute("CODE");
        try {
            dao.del(bmid,qid);
            gettm(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
